$(document).ready(function(){
    $('#submit').click(function(){
        var username = $('#username').val();
        var password = $('#password').val();
        var firstname = $('#firstname').val();
        var surname = $('#surname').val();
        var age = $('#age').val();
        var phone = $('#phone').val();
        var address = $('#address1').val();
        var crossStreet = $('#crossStreet').val();
        var email = $('#email').val();
        var dob = $('#dob').val();
        var pattern1 =/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
        var pattern2 = /^\D*0(\D*\d){9}\D*$/;
        if(pattern1.test(email) && pattern2.test(phone) && IsNumeric(age)) {

            var varData = 'username=' + username + '&password=' + password + '&email=' + email + '&firstname=' + firstname + '&surname=' + surname + '&age=' + age + '&phone=' + phone + '&address=' + address + '&crossStreet=' + crossStreet + '&dob=' + dob;
            console.log(varData);
            $.ajax({
                type: "POST",
                url: "http://localhost:8000/Uni/Project/Emailtest/registration.php",
                data: varData,
                success: function(data){
                    if(IsNumeric(data)){
                        sessionStorage.userID = data;
                        sessionStorage.username = username;
                        $("h1").before(sessionStorage.username);
                        $("form").remove();
                        $("button").remove();
                    }
                    else
                    {
                        //failure code
                        $("h1").before(data);
                    }
                }
            });
        }
        else{
            BadEmail();
        }
    });
});

function BadEmail(){
    $("h1").before("Bad input");
}

function IsNumeric(n) {
    return !isNaN(parseFloat(n)) && isFinite(n);
}